package com.tedu.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tedu.boot.entity.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class UserController {
    private static File userdir;
    static {
        userdir=new File("./users");
        if(!userdir.exists()){
            userdir.mkdirs();
        }
    }


    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String agestr=request.getParameter("age");
        if(username==null||username.trim().isEmpty()||
                password==null||password.trim().isEmpty()||
                nickname==null||nickname.trim().isEmpty()||
                agestr==null||agestr.trim().isEmpty()||
                !agestr.matches("[0-9]+")){
            try {
                response.sendRedirect("reg_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        User user=new User(username,password,nickname,Integer.parseInt(agestr));
        File file=new File(userdir,username+".obj");
        if(file.exists()){
            try {
                response.sendRedirect("have_user.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }


        try (
                FileOutputStream fos=new FileOutputStream(file);
                ObjectOutputStream oos=new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);
            response.sendRedirect("reg_success.html");
            } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if(username==null||username.trim().isEmpty()||
                password==null||password.trim().isEmpty()){
            try {
                response.sendRedirect("login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        File user=new File(userdir,username+".obj");
        if(!user.exists()){
            try {
                response.sendRedirect("login_name_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (
             FileInputStream fis=new FileInputStream(user);
             ObjectInputStream ois=new ObjectInputStream(fis);
             ){
            if(((User) ois.readObject()).getPassword().equals(password)){
                response.sendRedirect("usccess.html");
                return;
            }
            response.sendRedirect("login_info_error.html");
            return;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
